#define DEBUG
#pragma GCC optimize("-Ofast")
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=40000000;

long long sqr(long long a) {
  return a*a;
}

void gen(long long *a) {
  const int mods=1<<30;
  long long x, y, z, m, p, l, r;
  static long long b[3];
  scanf("%lld %lld %lld %lld %lld %lld", &x, &y, &z, b+1, b+2, &m);
  for (int i=1, j=1; i<=m; i++) {
    scanf("%lld %lld %lld", &p, &l, &r);
    for (; j<=p; j++) {
      if (j>2) {
	b[j%3] = ((x*b[(j-1)%3]%mods+y*b[(j-2)%3]%mods)%mods+z)%mods;
      }
      a[j] = a[j-1]+(b[j%3] % (r-l+1))+l;
    }
  }
}

long long calcSuf(long long *suf, int l, int r) {
  return 2*suf[r]-suf[l];
}

class Bignum {
public:
  const long long maxk=1000000000;
  long long a[6];

  void init(long long x) {
    memset(a, 0, sizeof a);
    for (; x>0; x/=maxk) {
      a[++a[0]] = x%maxk;
    }
  }

  void add(Bignum &x) {
    for (int i=1; i<=x.a[0]; i++) {
      a[i] += x.a[i];
      a[i+1] += a[i]/maxk;
      a[i] %= maxk;
    }
    for (int i=x.a[0]+1; i<=a[0]; i++) {
      a[i+1] += a[i]/maxk;
      a[i] %= maxk;
    }
    a[0] = a[0]<x.a[0] ? (a[x.a[0]+1] ? x.a[0]+1 : x.a[0]) : (a[a[0]+1] ? a[0]+1 : a[0]);
  }

  void sqr(Bignum &x) {
    memset(a, 0, sizeof a);
    for (int i=1; i<=x.a[0]; i++) {
      for (int j=1; j<=x.a[0]; j++) {
	a[i+j-1] += x.a[i]*x.a[j];
	a[i+j] += a[i+j-1]/maxk;
	a[i+j-1] %= maxk;
      }
    }
    a[0] = a[x.a[0]*2] ? x.a[0]*2 : x.a[0]*2-1;
  }

  void print() {
    printf("%lld", a[a[0]]);
    for (int i=a[0]-1; i; i--) {
      printf("%09lld", a[i]);
    }
  }
};

int main() {
  freopen("partition.in", "r", stdin);
  freopen("partition.out", "w", stdout);

  static long long suf[maxn+1];
  int n, type;
  scanf("%d %d", &n, &type);

  if (type) {
    gen(suf);
  } else {
    for (int i=1; i<=n; i++) {
      long long a;
      scanf("%lld", &a);
      suf[i] = a+suf[i-1];
    }
  }

  static int f[maxn+1], w[maxn+1];
  int h=1, t=1;
  for (int i=1; i<=n; i++) {
    for (; h<t && calcSuf(suf, f[w[h+1]], w[h+1])<=suf[i]; h++);
    f[i] = w[h];
    long long x=2*suf[i]-suf[f[i]];
    for (; t>=h && calcSuf(suf, f[w[t]], w[t])>x; t--);
    w[++t] = i;
  }

  static Bignum ans, temp[2];
  for (int i=n; i; i=f[i]) {
    temp[0].init(suf[i]-suf[f[i]]);
    temp[1].sqr(temp[0]);
    ans.add(temp[1]);
  }

  ans.print();

  fclose(stdin);
  fclose(stdout);
  return 0;
}
